package com.shelpe.services.sinbad.entity;

import java.util.Date;

import javax.persistence.AttributeConverter;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import lombok.Data;

@Entity(name = "taobao_top_keywords")
@Data
public class TaobaoTopKeyword {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	private long autoID;
	@Column(name = "leaf_id")
	private Long leafID;
	@Convert(converter = TypeConverter.class)
	private Type type;

	private Integer position;
	private String word;
	private String url;
	@Column(name = "look_index")
	private String lookIndex;
	@Column(name = "up_or_down")
	private Integer upOrDown;
	@Column(name = "change_num")
	private Integer change;
	private String ranges;
	@Temporal(TemporalType.DATE)
	private Date createTime;
	@Temporal(TemporalType.DATE)
	private Date syncTime;

	public static enum Type {
		up("up"), hot("hot");

		private String value;

		private Type(String v) {
			this.value = v;
		}

		public String getValue() {
			return this.value;
		}

		public static Type from(String value) {
			Type en = Type.up;
			for (Type s : Type.values()) {
				if (s.getValue().equals(value)) {
					en = s;
					break;
				}
			}
			return en;
		}
	}

	public static class TypeConverter implements AttributeConverter<Type, String> {

		@Override
		public String convertToDatabaseColumn(Type attribute) {
			return attribute != null ? attribute.getValue() : Type.up.getValue();
		}

		@Override
		public Type convertToEntityAttribute(String dbData) {
			return Type.from(dbData);
		}
	}
}
